//
//  Copyright (c) 2021 Open Whisper Systems. All rights reserved.
//

#import "TSGroupMember.h"
#import <SignalServiceKit/SignalServiceKit-Swift.h>

@interface TSGroupMember ()

@property (nonatomic, nullable) NSString *uuidString;
@property (nonatomic, nullable) NSString *phoneNumber;
@property (nonatomic) NSString *groupThreadId;
@property (nonatomic) uint64_t lastInteractionTimestamp;

@end

@implementation TSGroupMember

- (instancetype)initWithAddress:(SignalServiceAddress *)address
                  groupThreadId:(NSString *)groupThreadId
       lastInteractionTimestamp:(uint64_t)lastInteractionTimestamp
{
    if (self = [super init]) {
        _uuidString = address.uuidString;
        _phoneNumber = address.phoneNumber;
        _groupThreadId = groupThreadId;
        _lastInteractionTimestamp = lastInteractionTimestamp;
    }
    return self;
}

// --- CODE GENERATION MARKER

// This snippet is generated by /Scripts/sds_codegen/sds_generate.py. Do not manually edit it, instead run
// `sds_codegen.sh`.

// clang-format off

- (instancetype)initWithGrdbId:(int64_t)grdbId
                      uniqueId:(NSString *)uniqueId
                   groupThreadId:(NSString *)groupThreadId
        lastInteractionTimestamp:(uint64_t)lastInteractionTimestamp
                     phoneNumber:(nullable NSString *)phoneNumber
                      uuidString:(nullable NSString *)uuidString
{
    self = [super initWithGrdbId:grdbId
                        uniqueId:uniqueId];

    if (!self) {
        return self;
    }

    _groupThreadId = groupThreadId;
    _lastInteractionTimestamp = lastInteractionTimestamp;
    _phoneNumber = phoneNumber;
    _uuidString = uuidString;

    return self;
}

// clang-format on

// --- CODE GENERATION MARKER

- (SignalServiceAddress *)address
{
    return [[SignalServiceAddress alloc] initWithUuidString:self.uuidString phoneNumber:self.phoneNumber];
}

- (void)updateWithLastInteractionTimestamp:(uint64_t)lastInteractionTimestamp
                               transaction:(SDSAnyWriteTransaction *)transaction
{
    [self anyUpdateWithTransaction:transaction
                             block:^(TSGroupMember *groupMember) {
                                 groupMember.lastInteractionTimestamp = lastInteractionTimestamp;
                             }];
}

@end
